package com.jzo2o.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.health.mapper.ReservationSettingMapper;
import com.jzo2o.health.model.domain.ReservationSetting;
import com.jzo2o.health.model.dto.request.ReservationSettingUpsertReqDTO;
import com.jzo2o.health.model.dto.response.ReservationDateResDTO;
import com.jzo2o.health.model.dto.response.ReservationSettingResDTO;
import com.jzo2o.health.service.IReservationSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author DarkClown
 * @date 2024-08-26 22:07
 * @project_name jzo2o-health
 * @parem
 **/
@Service
public class IReservationSettingServiceImpl
     extends ServiceImpl<ReservationSettingMapper, ReservationSetting> implements IReservationSettingService  {

    @Autowired
    private ReservationSettingMapper reservationSettingMapper;

    @Override
    public List<ReservationSettingResDTO> getReservationSettingByMonth(String date) {
        List<ReservationSettingResDTO> reservationSettingResDTOList = reservationSettingMapper.getReservationSettingByMonth(date);
        return reservationSettingResDTOList;
    }

    @Override
    public void editNumberByDate(ReservationSettingUpsertReqDTO reservationSettingUpsertReqDTO) {

        //根据日期查询预约设置

        QueryWrapper<ReservationSetting> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda().eq(ReservationSetting::getOrderDate,reservationSettingUpsertReqDTO.getOrderDate());

        ReservationSetting reservationSetting = reservationSettingMapper.selectOne(queryWrapper);
        ReservationSetting reservationSetting1 = new ReservationSetting();
        if (reservationSetting == null&&reservationSettingUpsertReqDTO.getNumber()!=null){
            reservationSetting1.setOrderDate(reservationSettingUpsertReqDTO.getOrderDate());
            reservationSetting1.setNumber(reservationSettingUpsertReqDTO.getNumber());
            reservationSetting1.setReservations(0);
            int i = reservationSettingMapper.insert(reservationSetting1);
            //如果i为0，说明没有更新成功，则更新失败
            if (i == 0){
                throw new RuntimeException("更新失败");
            }
            return;
        }


         reservationSetting1 = reservationSetting.setNumber(reservationSettingUpsertReqDTO.getNumber());

        int i = reservationSettingMapper.updateById(reservationSetting1);


        //如果i为0，说明没有更新成功，则更新失败
        if (i == 0){
            throw new RuntimeException("更新失败");
        }

    }

    @Override
    public ReservationDateResDTO getReservationDateByMonth(String month) {

        ReservationDateResDTO reservationDateResDTO = new ReservationDateResDTO();
        List<String> reservationDateByMonth = reservationSettingMapper.getReservationDateByMonth(month);

        reservationDateResDTO.setDateList(reservationDateByMonth);


        return reservationDateResDTO;
    }
}
